<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>

<script language = "JavaScript">

		//**** List Amphur (Start) ***//
		function ListSub(SelectValue)
		{
			document.formedit.category_sub_id.length = 0;

			//*** Insert null Default Value ***//
			var myOption = new Option('','');

			
			<?php
			$strSQL = "SELECT * FROM category_sub ORDER BY category_sub_name ASC";
			$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
			$intRows = 0;
			while($objResult = mysql_fetch_array($objQuery))
			{
			$intRows++;
			?>
				x = <?php echo $intRows; ?>;
				mySubList = new Array();
				
				strGroup = <?php echo $objResult["category_id"];?>;
				strValue = "<?php echo $objResult["category_sub_id"];?>";
				strItem = "<?php echo $objResult["category_sub_name"];?>";
				mySubList[x,0] = strItem;
				mySubList[x,1] = strGroup;
				mySubList[x,2] = strValue;
							
				if (mySubList[x,1] == SelectValue){
					var myOption = new Option(mySubList[x,0], mySubList[x,2])  ;
					document.formedit.category_sub_id.options[document.formedit.category_sub_id.length]= myOption		;			
				}
			<?php
			}
			?>																	
		}
		//**** List Amphur (End) ***//


	function setFocus(){
		document.formedit.name.focus();
	}
	
</script>
</head>
<body onload="setFocus();" >
<?php

//แก้ไข
if($_GET["action"]=="update") {

	if($_POST['name'] != "" AND $_POST['unit_id'] != "" AND $_POST['category_id'] != "" AND $_POST['category_sub_id'] != "")
		{
			$Query = "UPDATE product SET product_name = '".$_POST["name"]."',unit_id = '".$_POST["unit_id"]."',category_id = '".$_POST["category_id"]."',category_sub_id = '".$_POST["category_sub_id"]."' WHERE product_id = '".$_POST["product_id"]."' ";
			$objQuery = mysql_query($Query);
			
			echo "<script type=\"text/javascript\"> window.location=\"product_manage.php\"</script>";
			exit();
		}
}

$Query = "SELECT * FROM product WHERE  product_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);
?>
<form action="?action=update" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>แก้ไขข้อมูลสินค้า</h1>
    <div class="buttons"><a onclick="formedit.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'product_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input name="name" type="text" id="name" value="<?php echo $row["product_name"];?>" /><input type="hidden" name="product_id" value="<?php echo $_GET['id']; ?>" /></td>
      </tr>
      <tr>
        <td>หน่วยนับ : </td>
        <td><select name="unit_id" id="unit_id">
        <option value=""><-- Please Select --></option>
		<?php
			$Querycate = "SELECT * FROM unit ORDER BY unit_name";
			$objQuerycate = mysql_query($Querycate);
			while($row1 = mysql_fetch_array($objQuerycate))
			{
		?>
          <option <?php if($row["unit_id"]==$row1["unit_id"]) {echo "selected";} ?> value="<?php echo $row1["unit_id"];?>"><?php echo "  ".$row1["unit_name"];?></option>
                <?php
			}
			?>
        </select></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><select name="category_id" id="category_id" onChange = "ListSub(this.value)" >
        <option value=""><-- Please Select --></option>
		<?php
			$Querycate = "SELECT * FROM category ORDER BY category_name";
			$objQuerycate = mysql_query($Querycate);
			while($row1 = mysql_fetch_array($objQuerycate))
			{
		?>
          <option <?php if($row["category_id"]==$row1["category_id"]) {echo "selected";} ?> value="<?php echo $row1["category_id"];?>"><?php echo "  ".$row1["category_name"];?></option>
                <?php
			}
			?>
        </select></td>
      </tr>
      <tr>
        <td>หมวดสินค้าย่อย : </td>
        <td><select name="category_sub_id" id="category_sub_id" style="min-width:140px;" ><option value=""><-- Please Select -->		
        <?php
			$Querycatesub = "SELECT * FROM category_sub WHERE category_id = '".$row["category_id"]."' ORDER BY category_sub_name";
			$objQuerycatesub = mysql_query($Querycatesub);
			while($rowsub = mysql_fetch_array($objQuerycatesub))
			{
		?>
          <option <?php if($row["category_sub_id"]==$rowsub["category_sub_id"]) {echo "selected";} ?> value="<?php echo $rowsub["category_sub_id"];?>"><?php echo "  ".$rowsub["category_sub_name"];?></option>
                <?php
			}
			?></option></select></td>
      </tr>
    </table>
  </div>
</div>
</form>
</body>
<?php mysql_close(); ?>
</html>
